$(function () {
	if ($("#courses").length > 0) {
		window.setInterval(listCourses, 1000);
	}
	if ($("#categories").length > 0) {
		window.setInterval(listCategories, 1000);
	}
	if ($("#questions").length > 0) {
		window.setInterval(listQuestions, 1000);
	}
	if ($("#clarifications").length > 0) {
		window.setInterval(listClarifications, 1000);
	}
	
	function listCourses() {
		$.post("/listCourses", function(data) {
			for (var i = 0; i < data.length; i++) {
				if ($("#" + data[i].key).length < 1) {
					$("#coursePageInfo").after(
					"<br /><div class=\"entry\"><span><a id=\"" 
					+ data[i].key + "\" class=\"entryHeading\" href=\"/course/" 
					+ data[i].key + "\">" + data[i].name + "</a>: " 
					+ data[i].description + "</span></div>"
					);
				}
			}
		}, "json");
	}
	
	function listCategories() {
		$.post("/listCategories", {courseKey: $("#storeCourse").attr("title")}, 
		function(data) {
			for (var i = 0; i < data.length; i++) {
				if ($("#" + data[i].key).length < 1 && data[i].name != "Default") {
					$("#categoryPageInfo").after(
					"<br /><div class=\"entry\"><span><a id=\"" 
					+ data[i].key + "\" class=\"entryHeading\" href=\"/category/" 
					+ data[i].key + "\">" + data[i].name + "</a>: " 
					+ data[i].subtitle + "</span></div>"
					);
				}
			}
		}, "json" );
	}
	
	function listQuestions() {
		$.post("/listQuestions", {courseKey: $("#storeCourse").attr("title"),
		categoryKey: $("#storeCategory").attr("title")}, 
		function(data) {
			categoryKey = $("#storeCategory").attr("title");
			for (var i = 0; i < data.length; i++) {
				if ($("#" + data[i].key).length < 1) {
					$("#questionPageInfo").after(
					"<br /><div class=\"entry\"><span><a id=\"" 
					+ data[i].key + "\" class=\"entryHeading\" href=\"/category/" 
					+ categoryKey + "/question/" + data[i].key + "\">" 
					+ data[i].name + "</a>: " + data[i].text 
					+ "</span></div>"
					);
				}
			}
		}, "json");
	}
	
	function listClarifications() {
		$.post("/listClarifications", {courseKey: $("#storeCourse").attr("title")}, 
		function(data) {
			for (var i = 0; i < data.length; i++) {
				if ($("#" + data[i].key).length < 1 && data[i].name != "Default") {
					$("#clarificationPageInfo").after(
					"<br /><div class=\"entry\" id=\"" + data[i].key + "\">" + data[i].name + ": " 
					+ data[i].text + "&nbsp; - asked by " + data[i].student + "</div>"
					);
				}
			}
		}, "json" );
	}
});
	